package com.aaa.utils;

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;

import java.io.FileOutputStream;
import java.util.UUID;

/**
 * @author : luoBo
 */
public class SmbImportUtil {

    public static void main(String[] args) throws Exception{
        // SMB 共享路径
        String smbPath = "smb://10.10.80.200/share/nationalReport/dev/古法单件调拨码表.xls";
        //String username = "your-username";
        //String password = "your-password";

        // 创建 SMB 文件对象
        //String authPath = "smb://" + username + ":" + password + "@10.10.80.200/share/nationalReport/dev/古法单件调拨码表.xls";
        SmbFile smbFile = new SmbFile(smbPath);

        // 检查文件是否存在
        if (!smbFile.exists()) {
            throw new RuntimeException("SMB 文件不存在！");
        }

        // 创建本地文件输出流
        String localFilePath = "D:/templates/" + UUID.randomUUID() + ".xls";
        try (SmbFileInputStream smbInputStream = new SmbFileInputStream(smbFile);

             FileOutputStream fileOutputStream = new FileOutputStream(localFilePath)) {

            // 缓冲区读取文件内容
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = smbInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }
        }

        System.out.println("文件下载成功！保存路径：" + localFilePath);

    }
}
